import random, os

levels = []

def isPair(hand):
	lcounts = [0]*13
	for i, l in enumerate(levels):
		lcounts[i] = [h[0] for h in hand].count(l)
	return any(c == 2 for c in lcounts)
	
def isTriple(hand):
	lcounts = [0]*13
	for i, l in enumerate(levels):
		lcounts[i] = [h[0] for h in hand].count(l)
	return any(c >= 3 for c in lcounts)
	return any(c >= 3 for c in [[h[0] for h in hand].count(l) for l in levels])

def isStraight(hand):
	shand = sorted([h[1] for h in hand])
	return shand == range(shand[0], shand[-1]+1)

def isFlush(hand):
	suitlist = [h[2] for h in hand]
	return suitlist.count(suitlist[0]) == len(suitlist)

def isFullHouse(hand):
	return isPair(hand) and isTriple(hand)
	
def isStraightFlush(hand):
	return isStraight(hand) and isFlush(hand)

def isRoyalFlush(hand):
	shand = sorted([h[1] for h in hand])
	return (shand in [range(9,14), range(10,15)]) and isFlush(hand)

cardtypes = [ 
("Ace", 14), 
("King", 13), 
("Queen", 12), 
("Jack", 11), 
("Ten", 10), 
("Nine", 9), 
("Eight", 8), 
("Seven", 7), 
("Six", 6), 
("Five", 5), 
("Four", 4), 
("Three", 3), 
("Two", 2)]

suits = ['diamonds','hearts','clubs','spades']
deck = []
for t in cardtypes:
	for s in suits:
		if s == 'spades':
			levels.append(t[0].lower())
		deck.append((t[0].lower(),t[1],s))
		
random.shuffle(deck)

chips = 0
checks = [isPair, isTriple, isStraight, isFlush, isFullHouse, isStraightFlush, isRoyalFlush]
prizes = range(25,25*8,25)
going = True
while(going):
	myhand = [deck.pop(0) for x in range(0,5)]
	print '\n'.join([c[0] + ' of ' + c[2] for c in myhand])
	opt = raw_input("h for hold, r for redraw, s to stop: ")
	if opt == 'h':
		prize = 0
		for p, check in enumerate(checks):
			if check(myhand):
				prize = prizes[p]
		os.system('clear')
		print 'you have won '+str(prize)+' chips!'
		chips += prize
	elif opt == 'r':
		os.system('clear')
	else:
		print 'final chip balance: ' + str(chips)
		going = False
	deck += [myhand.pop(0) for x in range(0,5)]
	random.shuffle(deck)
	
